package org.example.Server.controller.home;

import lombok.extern.slf4j.Slf4j;
import org.example.Server.model.home.CommentDTO;
import org.example.Server.model.home.NewReplyDTO;
import org.example.Server.model.home.Post;
import org.example.Server.model.home.TypeReplyVO;
import org.example.Server.service.home.HomeService;
import org.example.Server.utils.PageResult;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/youxich/home")
public class HomeCommentController {
    @Autowired
    private HomeService homeService;

    /**
     * 话题展示（最新回复按照拥有最新回复的时间进行话题分页展示）
     *
     * @param newReplyDTO
     * @return
     */
    @GetMapping("/topic/newreply")
    public ResultAPI<PageResult> getNewReply(NewReplyDTO newReplyDTO) {
        PageResult page = homeService.getNewReply(newReplyDTO);
        return ResultAPI.success(page);
    }

    /**
     * 话题展示（热门话题按照拥有的话题回复数量从多到少排序进行话题分页展示
     *
     * @param newReplyDTO
     * @return
     */
    @GetMapping("/topic/hot")
    public ResultAPI<PageResult> getHotReply(NewReplyDTO newReplyDTO) {
        PageResult page = homeService.getHotReply(newReplyDTO);
        return ResultAPI.success(page);
    }

    /**
     * 话题展示（技术分享、通知公告
     * 按照话题类型进行话题分页展示（技术分享type=discussion、通知公告type=notice）
     * <p>
     * 默认按照更新时间降序,无视置顶
     *
     * @param newReplyDTO
     * @return
     */
    @GetMapping("topic")
    public ResultAPI<PageResult> getTypeReply(NewReplyDTO newReplyDTO) {
        PageResult page = homeService.getTypeReply(newReplyDTO);
        return ResultAPI.success(page);
    }

    /**
     * 删除话题
     *
     * @param id
     * @return
     */
    @DeleteMapping("/topic/{id}")
    public ResultAPI<Boolean> deleteTopic(@PathVariable Integer id) {
        return homeService.deleteTopic(id);
    }


    /**
     * 发布话题
     *
     * @param post
     * @return
     */
    @PostMapping("topic")
    public ResultAPI setTopic(@RequestBody Post post) {
        homeService.setTopic(post);
        return ResultAPI.success();
    }

    /**
     * 根据post_id进行单个话题详情展示
     *
     * @param postId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/topic/{post_id}")
    public ResultAPI<TypeReplyVO> getDetailReply(@PathVariable("post_id") int postId,
                                                 @RequestParam(value = "page", defaultValue = "1") int page,
                                                 @RequestParam(value = "pagesize", defaultValue = "10") int pageSize) {
        TypeReplyVO typeReplyVO = homeService.getDetailReply(postId, page, pageSize);
        return ResultAPI.success(typeReplyVO);
    }

    /**
     * 进行帖子回复
     *
     * @param commentDTO
     * @return
     */
    @PostMapping("/comment")
    public ResultAPI CommentReply(@RequestBody CommentDTO commentDTO) {
        return homeService.CommentReplyPost(commentDTO);
    }

    @DeleteMapping("/comment/{id}")
    public ResultAPI deleteComment(@PathVariable("id") int id) {
        return homeService.deleteComment(id);
    }
}
